<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
  <meta name="referrer" content="never" />
  
  
  <meta name="description" content="用于个人" />
  

  
  
  <meta name="google-site-verification" content="NCXVSqxqB-os803-VFMtIEd1SUNJVOIjctCfNYUwD0w" />
  <meta name="baidu-site-verification" content="Nzwx7HuplM" />
  
  <meta name="msvalidate.01" content="9E9EA92F7E5327E1FE8A5196D5253664" />
  
  
  
  <title>Redis | masuo777的博客</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="用于个人">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis">
<meta property="og:url" content="https://masuo777.github.io/2022/01/11/Redis/index.html">
<meta property="og:site_name" content="masuo777的博客">
<meta property="og:description" content="用于个人">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112093215.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112095821.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112101441.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112104407.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112104352.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112094022.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112094628.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112084849.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112084903.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112085053.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112085147.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112111208.png">
<meta property="og:image" content="g:/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/image/image-20220112144646499.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112151015.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112151351.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112152433.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112153138.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112160658.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161008.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161334.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161649.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112162347.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112170204.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112171628.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112172925.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112112719.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220113092955.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220113085922.png">
<meta property="og:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220121090117.png">
<meta property="article:published_time" content="2022-01-11T08:39:18.401Z">
<meta property="article:modified_time" content="2022-03-08T03:08:02.816Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112093215.png">
  
    <link rel="alternative" href="/atom.xml" title="masuo777的博客" type="application/atom+xml">
  
  
    <link rel="icon" href="http://7u2hdm.com1.z0.glb.clouddn.com/favicon.ico">
  
  
<link rel="stylesheet" href="/css/style.css">

  

  
  <!-- baidu webmaster push -->
  <script src='//push.zhanzhang.baidu.com/push.js'></script>
<meta name="generator" content="Hexo 5.4.0"></head>
<body class="home blog custom-background custom-font-enabled single-author">
  <div id="page" class="hfeed site">
      <header id="masthead" class="site-header" role="banner">
    <hgroup>
      <h1 class="site-title">
        <a href="/" title="masuo777的博客" rel="home">masuo777的博客</a>
      </h1>
      
        <script type="text/javascript" src="http://api.hitokoto.us/rand?encode=js&charset=utf-8"></script>
        <h2 class="site-description"><script>hitokoto();</script></h2>
      
    </hgroup>

    <nav id="site-navigation" class="main-navigation" role="navigation">
            <button class="menu-toggle">菜单</button>
            <a class="assistive-text" href="/#content" title="跳至内容">跳至内容</a><!--TODO-->
            <div class="menu-main-container">
                <ul id="menu-main" class="nav-menu">
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/">MDS</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/todo">ToDo</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/images">images</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/about">ME</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/works">作品集</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a href="/tools">工具集</a></li>
                
                    <li class="menu-item menu-item-type-post_type menu-item-object-page"><a target="_blank" rel="noopener" href="https://github.com/Masuo777">my github</a></li>
                
                </ul>
            </div>
    </nav>
</header>
      <div id="main" class="wrapper">
        <div id="primary" class="site-content"><div id="content" role="main"><article id="post-Redis" class="post-Redis post type-post status-publish format-standard hentry">
    <!---->

      <header class="entry-header">
        
        
  
    <h1 class="entry-title article-title">
      Redis
    </h1>
  

        
        <div class="comments-link">
            
            <a href="/2022/01/11/Redis/#comments" class="leave-reply">评论</a>
            
            <a href="javascript:void(0);" data-url="https://masuo777.github.io/2022/01/11/Redis/" data-id="cl3jit8no000f2ojb7vft7xlt" class="leave-reply bdsharebuttonbox" data-cmd="more">分享</a>
        </div><!-- .comments-link -->
      </header><!-- .entry-header -->

    <div class="entry-content">
      
        <h1 id="Redis"><a href="#Redis" class="headerlink" title="Redis"></a>Redis</h1><blockquote>
<p>REmote DIctionary Server(Redis) 是一个由 Salvatore Sanfilippo 写的 key-value 存储系统，是跨平台的非关系型数据库。</p>
<p>Redis 是一个开源的使用 ANSI C 语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式、可选持久性的键值对(Key-Value)存储数据库，并提供多种语言的 API。</p>
<p>Redis 通常被称为数据结构服务器，因为值（value）可以是字符串(String)、哈希(Hash)、列表(list)、集合(sets)和有序集合(sorted sets)等类型。</p>
<p>Redis是一种nosql数据库，他的数据保存在内存中，同时他可以定期将数据存入磁盘中，实现数据持久化。</p>
</blockquote>
<span id="more"></span>

<p>官网：<a href="redis.io">redis</a>.</p>
<h2 id="优势"><a href="#优势" class="headerlink" title="优势"></a>优势</h2><ul>
<li>支持数据持久化，将内存中的数据保存在硬盘中，重启时可再次加载</li>
<li>读写性能高，Redis能读的速度是110000次/s,写的速度是81000次/s </li>
<li>支持的数据类型广泛，String，key-value，list，set，zset，hash等</li>
<li>支持数据备份，master-slave（主从模式）</li>
<li>原子性，Redis的所有操作都是原子性的，且支持事务</li>
<li>支持publish/subscribe通知</li>
</ul>
<h2 id="安装"><a href="#安装" class="headerlink" title="安装"></a>安装</h2><p>在<a target="_blank" rel="noopener" href="http://redis.cn/download.html">Redis中国站</a>下载压缩包解压到本地即可。</p>
<p>开箱即用。</p>
<p>Linux与Windows要注意。</p>
<p>Linux版本遍地都是。</p>
<p>Windows版本停留在了2016年。<a target="_blank" rel="noopener" href="https://github.com/microsoftarchive/redis/tags">传送门</a>。——截止2022.1.11</p>
<h3 id="Redis服务端"><a href="#Redis服务端" class="headerlink" title="Redis服务端"></a>Redis服务端</h3><blockquote>
<p>首先，下载完之后我们需要启动Redis服务。当然安装版不需要安装服务。</p>
<p>启动redis服务有两种方式：</p>
<p>1、安装redis服务，一劳永逸，且简单方便。</p>
<p>2、在需要使用redis服务时在启动redis服务，费事但是安全。</p>
</blockquote>
<h4 id="一、安装版"><a href="#一、安装版" class="headerlink" title="一、安装版"></a>一、安装版</h4><h5 id="安装Redis服务"><a href="#安装Redis服务" class="headerlink" title="安装Redis服务"></a>安装Redis服务</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在redis安装目录下执行下面命令，提示successfully就说明安装完成</span></span><br><span class="line">redis-server.exe --service-install redis.windows.conf --loglevel verbose</span><br><span class="line"><span class="comment"># 开启服务</span></span><br><span class="line">redis-server.exe --service-start</span><br><span class="line"><span class="comment"># 关闭服务</span></span><br><span class="line">redis-server.exe --service-stop</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112093215.png" alt="image-20220112093214143"></p>
<h5 id="删除Redis服务"><a href="#删除Redis服务" class="headerlink" title="删除Redis服务"></a>删除Redis服务</h5><blockquote>
<p>当我们不需要Redis服务端后，我们需要删除服务端。</p>
<p>或者想要更换一个服务端，我们也需要删除服务端。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除服务端，就是删除Windows的一个服务，删除Windows服务需要管理员的权限，所以我们以管理员的身份打开cmd窗口</span></span><br><span class="line">sc delete Redis</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112095821.png" alt="image-20220112095820079"></p>
<h4 id="二、启动版"><a href="#二、启动版" class="headerlink" title="二、启动版"></a>二、启动版</h4><h5 id="启动Redis服务"><a href="#启动Redis服务" class="headerlink" title="启动Redis服务"></a>启动Redis服务</h5><blockquote>
<p>启动redis服务，需要保证Redis服务没有启动，这个Redis服务指的是你上面安装的那个，可以在Windows服务中找到的，需要先停止服务，不然会冲突。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 找到redis安装目录</span></span><br><span class="line">redis-server.exe redis.windows.conf</span><br><span class="line"><span class="comment"># 此时redis服务就启动了，注意不要关闭该页面，不然服务就中断了，需要另开一个页面进行操作</span></span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112101441.png" alt="image-20220112101439787"></p>
<h3 id="连接客户端"><a href="#连接客户端" class="headerlink" title="连接客户端"></a>连接客户端</h3><blockquote>
<p>连接本地服务以及连接远程服务</p>
</blockquote>
<h4 id="连接本地客户端"><a href="#连接本地客户端" class="headerlink" title="连接本地客户端"></a>连接本地客户端</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 连接本地客户端</span></span><br><span class="line">redis-cli</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112104407.png" alt="image-20220112104406576"></p>
<h4 id="连接远程服务端"><a href="#连接远程服务端" class="headerlink" title="连接远程服务端"></a>连接远程服务端</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 连接远程服务</span></span><br><span class="line"><span class="comment"># redis-cli -h &lt;ip&gt; -p &lt;port&gt; -a &lt;password&gt;</span></span><br><span class="line"><span class="comment"># 连接地址为192.168.64.134 端口为6379 密码为123456 的远程redis服务</span></span><br><span class="line">redis-cli -h 192.168.64.134 -p 6379 -a 123456</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112104352.png" alt="image-20220112104351537"></p>
<h2 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h2><p>这里配不配环境变量都可以，配个环境变量只是为了方便让你在哪都能打开redis服务。</p>
<h2 id="配置"><a href="#配置" class="headerlink" title="配置"></a>配置</h2><h3 id="设置服务端密码"><a href="#设置服务端密码" class="headerlink" title="设置服务端密码"></a>设置服务端密码</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 找到安装目录下的redis.windows-server.conf,这里我们设置的是服务端密码，所以是带有server的配置文件</span></span><br><span class="line"><span class="comment"># 找到【requirepass foobared】，复制一行，去掉注释，将foobared改成你自己的密码</span></span><br><span class="line">requirepass 123456</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112094022.png" alt="image-20220112094021729"></p>
<p>下次登录时，我们就需要验证身份了。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 我们在登录客户端时,是正常的，</span></span><br><span class="line">redis-cli</span><br><span class="line"><span class="comment"># 但是当我们在访问服务端数据时，如果我们没有验证身份，就会报如下错误</span></span><br><span class="line">get userName </span><br><span class="line"><span class="comment"># 说明我们没有授权</span></span><br><span class="line">(error) NOAUTH Authentication required.</span><br><span class="line"><span class="comment"># 这时候我们需要主动发起授权认证</span></span><br><span class="line">auth &lt;password&gt;</span><br><span class="line"><span class="comment"># 之后再访问就没问题了</span></span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112094628.png" alt="image-20220112094627205"></p>
<h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><blockquote>
<p>Redis 命令用于在redis服务（即安装的redis服务）上执行操作。此服务在一台电脑上只会有一个。</p>
<p>要在redis服务上执行命令就需要一个redis客户端（即redis-cli）。客户端可以有多个。</p>
<p>客户端就是redis-cli开启的服务。</p>
<p>更多可参考：<a target="_blank" rel="noopener" href="http://redisdoc.com/index.html">http://redisdoc.com/index.html</a></p>
</blockquote>
<h3 id="服务端"><a href="#服务端" class="headerlink" title="服务端"></a>服务端</h3><blockquote>
<p>详情查看 [Redis服务端]( # Redis服务端)</p>
</blockquote>
<h4 id="开启服务端"><a href="#开启服务端" class="headerlink" title="开启服务端"></a>开启服务端</h4><blockquote>
<p>Redis 服务端分为本地服务和自启服务</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Windows 在redis安装目录下，如果你配置了环境变量就无所谓了</span></span><br><span class="line">redis-server.exe --service-start</span><br><span class="line"><span class="comment"># Linux</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h4 id="关闭服务端"><a href="#关闭服务端" class="headerlink" title="关闭服务端"></a>关闭服务端</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Windows 在redis安装目录下，如果你配置了环境变量就无所谓了</span></span><br><span class="line">redis-server.exe --service-stop</span><br><span class="line"><span class="comment"># Linux</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="客户端"><a href="#客户端" class="headerlink" title="客户端"></a>客户端</h3><blockquote>
<p>详情查看 [连接客户端](# 连接客户端)</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Windows</span></span><br><span class="line">redis-cli</span><br><span class="line"><span class="comment"># Linux</span></span><br><span class="line">redis-cli</span><br><span class="line"></span><br><span class="line"><span class="comment"># 连接成功就会进入如下命令行</span></span><br><span class="line">127.0.0.1:6379&gt;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 身份授权</span></span><br><span class="line">auth &lt;password&gt;</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112084849.png" alt="image-20220112084848022"></p>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112084903.png" alt="image-20220112084902000"></p>
<h3 id="键"><a href="#键" class="headerlink" title="键"></a>键</h3><blockquote>
<p>键往往是以键值对的形式进行操作。</p>
</blockquote>
<h4 id="设置键"><a href="#设置键" class="headerlink" title="设置键"></a>设置键</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Windows ==》 set &lt;键&gt; &lt;值&gt;,当你输入一个set后redis会提示你该输入什么东西</span></span><br><span class="line"><span class="built_in">set</span> user mas</span><br><span class="line"><span class="comment"># Linux</span></span><br><span class="line"><span class="built_in">set</span> user mas</span><br><span class="line"></span><br><span class="line"><span class="comment"># 取值 get &lt;键&gt;</span></span><br><span class="line">get user</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112085053.png" alt="image-20220112085052155"></p>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112085147.png" alt="image-20220112085145109"></p>
<h4 id="删除键值对"><a href="#删除键值对" class="headerlink" title="删除键值对"></a>删除键值对</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 删除键</span></span><br><span class="line"><span class="comment"># del &lt;键&gt;,删除成功就会返回 (integer)1,否则就会返回 (integer)2</span></span><br><span class="line">del user</span><br></pre></td></tr></table></figure>

<h4 id="KEYS"><a href="#KEYS" class="headerlink" title="KEYS"></a>KEYS</h4><blockquote>
<p>pattern有以下匹配模式</p>
<p>?：用于匹配单个字符</p>
<p>*：用于匹配零个或者多个字符</p>
<p>[]：可以用来指定模式的选择区间</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 找到匹配的键可以使用keys pattern</span></span><br><span class="line"><span class="comment"># 如 pattern = * 时表示匹配所有的key</span></span><br><span class="line">keys *</span><br><span class="line"><span class="comment"># 使用1*表示以1开头的所有的key</span></span><br><span class="line">keys 1*</span><br><span class="line"><span class="comment"># 使用*1表示以1结尾的所有的key</span></span><br><span class="line">keys *1</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112111208.png" alt="image-20220112111207500"></p>
<h4 id="移动键到指定的库"><a href="#移动键到指定的库" class="headerlink" title="移动键到指定的库"></a>移动键到指定的库</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 移动键到指定的库可以使用</span></span><br><span class="line"><span class="comment"># move &lt;key&gt; &lt;db&gt; 这里db用数字表示，1代表db1，因为在redis中</span></span><br></pre></td></tr></table></figure>



<h4 id="键常用命令总结"><a href="#键常用命令总结" class="headerlink" title="键常用命令总结"></a><strong>键</strong>常用命令总结</h4><table>
<thead>
<tr>
<th align="left">命令及描述</th>
<th></th>
</tr>
</thead>
<tbody><tr>
<td align="left">DEL k</td>
<td>该命令用于在 key 存在时删除 key。</td>
</tr>
<tr>
<td align="left">DUMP k</td>
<td>序列化给定 key ，并返回被序列化的值。</td>
</tr>
<tr>
<td align="left">EXISTS k</td>
<td>检查给定 key 是否存在</td>
</tr>
<tr>
<td align="left">EXPIRE</td>
<td>seconds 为给定 key 设置过期时间，以秒计。</td>
</tr>
<tr>
<td align="left">EXPIREAT k timestamp</td>
<td>EXPIREAT 的作用和 EXPIRE 类似，都用于为 key 设置过期时间。 不同在于 EXPIREAT 命令接受的时间参数是 UNIX 时间戳(unix timestamp)。</td>
</tr>
<tr>
<td align="left">PEXPIRE k milliseconds</td>
<td>设置 key 的过期时间以毫秒计。</td>
</tr>
<tr>
<td align="left">PEXPIREAT k milliseconds-timestamp</td>
<td>设置 key 过期时间的时间戳(unix timestamp) 以毫秒计</td>
</tr>
<tr>
<td align="left">KEYS pattern</td>
<td>查找所有符合给定模式( pattern)的 key ，即找到能和pattern进行匹配的keys</td>
</tr>
<tr>
<td align="left">MOVE k db</td>
<td>将当前数据库的 key 移动到给定的数据库 db 当中。</td>
</tr>
</tbody></table>
<h3 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h3><blockquote>
<p>通过上面了解，我们知道Redis支持多种数据的存储与处理，那么Redis是如何将这些数据存入到数据库以及存入数据库之后又是如何操作的呢？下面来一一展示。</p>
</blockquote>
<h4 id="查看类型"><a href="#查看类型" class="headerlink" title="查看类型"></a>查看类型</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看key的数据类型</span></span><br><span class="line">TYPE key</span><br></pre></td></tr></table></figure>

<h4 id="查看TTL"><a href="#查看TTL" class="headerlink" title="查看TTL"></a>查看TTL</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看key的TTL（Time To Live，剩余存活时间），-1代表无限，-2代表已死（已被删除）</span></span><br><span class="line">TTL key</span><br></pre></td></tr></table></figure>



<h4 id="字符串"><a href="#字符串" class="headerlink" title="字符串"></a>字符串</h4><blockquote>
<p>字符串是Redis最简单的数据类型，</p>
<p>参考自：<a target="_blank" rel="noopener" href="http://redisdoc.com/string/index.html">http://redisdoc.com/string/index.html</a></p>
</blockquote>
<h5 id="SET"><a href="#SET" class="headerlink" title="SET"></a>SET</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SET key value [EX seconds] [PX milliseconds] [EXAT timestamp] [PXAT milliseconds-timestamp] [KEEPTTL] [NX|XX]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
<p>将字符串值 <code>value</code> 关联到 <code>key</code> 。</p>
<p>如果 <code>key</code> 已经持有其他值， <code>SET</code> 就覆写旧值， 无视类型。</p>
<p>当 <code>SET</code> 命令对一个带有生存时间（TTL）的键进行设置之后， 该键原有的 TTL 将被清除。</p>
<p>可选参数</p>
<ul>
<li>EX：设置键值对存活的时间（s）</li>
<li>PX：设置键值对存活的时间（ms）</li>
<li>EXAT：设置以秒为单位的UNIX时间戳所对应的时间为过期时间</li>
<li>PXAT：设置以毫秒为单位的UNIX时间戳所对应的时间为过期时间</li>
<li>KEEPTTL：保留该键值对的过期时间</li>
<li>NX/XX：不存在/已存在的键值对</li>
</ul>
</blockquote>
<p><strong>返回值</strong></p>
<blockquote>
<p>在 Redis 2.6.12 版本以前， <code>SET</code> 命令总是返回 <code>OK</code> 。</p>
<p>从 Redis 2.6.12 版本开始， <code>SET</code> 命令只在设置操作成功完成时才返回 <code>OK</code> ； </p>
<p>如果命令使用了 <code>NX</code> 或者 <code>XX</code> 选项， 但是因为条件没达到而造成设置操作未执行， 那么命令将返回空批量回复（NULL Bulk Reply = nil）。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 清空数据库</span></span><br><span class="line">flushdb</span><br><span class="line"><span class="comment"># 查看数据库的键值对</span></span><br><span class="line">keys *</span><br><span class="line"><span class="comment"># 设置新键值对</span></span><br><span class="line">SET key value</span><br><span class="line"><span class="comment"># 设置已存在的键值对,覆盖以前的值</span></span><br><span class="line">SET key new-value</span><br><span class="line"><span class="comment"># 使用 EX 参数 - 设置键值对存活的时间（s）</span></span><br><span class="line">SET key-ex value EX 10</span><br><span class="line"><span class="comment"># 使用 PX 参数 - 设置键值对存活的时间（ms）</span></span><br><span class="line">SET key-px value PX 10000</span><br><span class="line"><span class="comment"># 使用 NX 参数 - 键不存在则创建成功，键存在则创建失败，失败返回nil</span></span><br><span class="line">SET key-nx value NX</span><br><span class="line"><span class="comment"># 使用 XX 参数 - 键存在则创建成功，不存在则创建失败，失败返回nil</span></span><br><span class="line">SET key-nx value XX</span><br><span class="line"><span class="comment"># 查看键剩余存活时间，正数代表还剩多长时间，-2代表不存在了，</span></span><br><span class="line">TTL key</span><br></pre></td></tr></table></figure>

<p><img src="G:/%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/image/image-20220112144646499.png" alt="image-20220112144646499"></p>
<h5 id="STRLEN"><a href="#STRLEN" class="headerlink" title="STRLEN"></a>STRLEN</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">STRLEN key</span><br></pre></td></tr></table></figure>

<blockquote>
<p>返回存储的字符串的长度</p>
<p><code>STRLEN</code> 命令返回字符串值的长度。</p>
<p>当键 <code>key</code> 不存在时， 命令返回 <code>0</code> 。</p>
<p>当 <code>key</code> 储存的不是字符串值时， 返回一个错误。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取存储的字符串的长度</span></span><br><span class="line">STRLEN　key</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112151015.png" alt="image-20220112151013877"></p>
<h5 id="APPEND"><a href="#APPEND" class="headerlink" title="APPEND"></a>APPEND</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">APPEND key value</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： 平摊O(1)</p>
<p>如果键 <code>key</code> 已经存在并且它的值是一个字符串， <code>APPEND</code> 命令将把 <code>value</code> 追加到键 <code>key</code> 现有值的末尾。</p>
<p>如果 <code>key</code> 不存在， <code>APPEND</code> 就简单地将键 <code>key</code> 的值设为 <code>value</code> ， 就像执行 <code>SET key value</code> 一样。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 当前key不存在</span></span><br><span class="line">APPEND key value</span><br><span class="line"><span class="comment"># 该命令会返回一个整数,代表该字符串拼接之后的长度,相当于在拼接之后执行了一个STRLEN命令</span></span><br><span class="line">APPEND key value</span><br><span class="line"></span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">GET key</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112151351.png" alt="image-20220112151350860"></p>
<h5 id="SETRANGE"><a href="#SETRANGE" class="headerlink" title="SETRANGE"></a>SETRANGE</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SETRANGE key offset value</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.2.0</p>
<p>从偏移量 <code>offset</code> 开始， 用 <code>value</code> 参数覆写(overwrite)键 <code>key</code> 储存的字符串值。</p>
<p>即offset之前的数据不变,offset之后的用value覆盖,value长度如果小于key-offset的长度,那么后面的数据不变</p>
<p>不存在的键 <code>key</code> 当作空白字符串处理。</p>
<p><code>SETRANGE</code> 命令会确保字符串足够长以便将 <code>value</code> 设置到指定的偏移量上， 如果键 <code>key</code> 原来储存的字符串长度比偏移量小(比如字符串只有 <code>5</code> 个字符长，但你设置的 <code>offset</code> 是 <code>10</code> )， 那么原字符和偏移量之间的空白将用零字节(zerobytes, <code>&quot;\x00&quot;</code> )进行填充。</p>
<p>因为 Redis 字符串的大小被限制在 512 兆(megabytes)以内， 所以用户能够使用的最大偏移量为 2^29-1(536870911) ， 如果你需要使用比这更大的空间， 请使用多个 <code>key</code> 。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以上面的key为例</span></span><br><span class="line">SETRANGE key 2 555</span><br><span class="line"><span class="comment"># 这里会返回修改后的数组长度</span></span><br><span class="line"><span class="comment"># 查看key</span></span><br><span class="line">GET key</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112152433.png" alt="image-20220112152432302"></p>
<h5 id="GETRANGE"><a href="#GETRANGE" class="headerlink" title="GETRANGE"></a>GETRANGE</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GETRANGE key start end</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.4.0</p>
<p>时间复杂度： O(N)，其中 N 为被返回的字符串的长度。</p>
<p>返回键 <code>key</code> 储存的字符串值的指定部分， 字符串的截取范围由 <code>start</code> 和 <code>end</code> 两个偏移量决定 (包括 <code>start</code> 和 <code>end</code> 在内)。</p>
<p>负数偏移量表示从字符串的末尾开始计数， <code>-1</code> 表示最后一个字符， <code>-2</code> 表示倒数第二个字符， 以此类推。</p>
<p><code>GETRANGE</code> 通过保证子字符串的值域(range)不超过实际字符串的值域来处理超出范围的值域请求。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 还是以上面的key为例</span></span><br><span class="line"><span class="comment"># 获取从2到最后一个字符</span></span><br><span class="line">GETRANGE key 2 -1</span><br><span class="line"><span class="comment"># 获取从0到倒数第二个字符</span></span><br><span class="line">GETRANGE key 0 -2</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112153138.png" alt="image-20220112153137369"></p>
<h5 id="INCR"><a href="#INCR" class="headerlink" title="INCR"></a>INCR</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">INCR key</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
<p>为键 <code>key</code> 储存的数字值加上一。</p>
<p>如果键 <code>key</code> 不存在， 那么它的值会先被初始化为 <code>0</code> ， 然后再执行 <code>INCR</code> 命令。</p>
<p>如果键 <code>key</code> 储存的值不能被解释为数字， 那么 <code>INCR</code> 命令将返回一个错误。</p>
<p>本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
</blockquote>
<blockquote>
<p><code>INCR</code> 命令是一个针对字符串的操作。 因为 Redis 并没有专用的整数类型， 所以键 <code>key</code> 储存的值在执行 <code>INCR</code> 命令时会被解释为十进制 64 位有符号整数。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 设置num值为10</span></span><br><span class="line">SET num 10</span><br><span class="line"><span class="comment"># 给num +1</span></span><br><span class="line">INCR num</span><br><span class="line"><span class="comment"># 给String +1 ，key是String类型，所以会抛出错误</span></span><br><span class="line">INCR key</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112160658.png" alt="image-20220112160647627"></p>
<h5 id="INCRBY"><a href="#INCRBY" class="headerlink" title="INCRBY"></a>INCRBY</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">INCRBY key increment</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
<p>为键 <code>key</code> 储存的数字值加上增量 <code>increment</code> 。</p>
<p>如果键 <code>key</code> 不存在， 那么键 <code>key</code> 的值会先被初始化为 <code>0</code> ， 然后再执行 <code>INCRBY</code> 命令。</p>
<p>如果键 <code>key</code> 储存的值不能被解释为数字， 那么 <code>INCRBY</code> 命令将返回一个错误。</p>
<p>本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以上面的num为例子，给num +5</span></span><br><span class="line">INCRBY num 5</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161008.png" alt="image-20220112161007502"></p>
<h5 id="INCRBYFLOAT"><a href="#INCRBYFLOAT" class="headerlink" title="INCRBYFLOAT"></a>INCRBYFLOAT</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">INCRBYFLOAT key increment</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.6.0</p>
<p>时间复杂度： O(1)</p>
<p>为键 <code>key</code> 储存的值加上浮点数增量 <code>increment</code> 。</p>
<p>如果键 <code>key</code> 不存在， 那么 <code>INCRBYFLOAT</code> 会先将键 <code>key</code> 的值设为 <code>0</code> ， 然后再执行加法操作。</p>
<p>如果命令执行成功， 那么键 <code>key</code> 的值会被更新为执行加法计算之后的新值， 并且新值会以字符串的形式返回给调用者。</p>
<p>无论是键 <code>key</code> 的值还是增量 <code>increment</code> ， 都可以使用像 <code>2.0e7</code> 、 <code>3e5</code> 、 <code>90e-2</code> 那样的指数符号(exponential notation)来表示， 但是， <strong>执行 INCRBYFLOAT 命令之后的值</strong>总是以同样的形式储存， 也即是， 它们总是由一个数字， 一个（可选的）小数点和一个任意长度的小数部分组成（比如 <code>3.14</code> 、 <code>69.768</code> ，诸如此类)， 小数部分尾随的 <code>0</code> 会被移除， 如果可能的话， 命令还会将浮点数转换为整数（比如 <code>3.0</code> 会被保存成 <code>3</code> ）。</p>
<p>此外， 无论加法计算所得的浮点数的实际精度有多长， <code>INCRBYFLOAT</code> 命令的计算结果最多只保留小数点的后十七位。</p>
<p>当以下任意一个条件发生时， 命令返回一个错误：</p>
<ul>
<li>键 <code>key</code> 的值不是字符串类型(因为 Redis 中的数字和浮点数都以字符串的形式保存，所以它们都属于字符串类型）；</li>
<li>键 <code>key</code> 当前的值或者给定的增量 <code>increment</code> 不能被解释(parse)为双精度浮点数。</li>
</ul>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以上面的num为例，给num +5.3</span></span><br><span class="line">INCRBYFLOAT num 5.3</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161334.png" alt="image-20220112161333378"></p>
<h5 id="DECR"><a href="#DECR" class="headerlink" title="DECR"></a>DECR</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DECR key</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
<p>为键 <code>key</code> 储存的数字值减去一。</p>
<p>如果键 <code>key</code> 不存在， 那么键 <code>key</code> 的值会先被初始化为 <code>0</code> ， 然后再执行 <code>DECR</code> 操作。</p>
<p>如果键 <code>key</code> 储存的值不能被解释为数字， 那么 <code>DECR</code> 命令将返回一个错误。</p>
<p>本操作的值限制在 64 位(bit)有符号数字表示之内。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以上面的num为例,发现他不能对浮点数进行操作，可能是因为存在小数点被误认为是String类型，我们需要先将其弄成整数</span></span><br><span class="line">DECR num</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112161649.png" alt="image-20220112161648262"></p>
<h5 id="DECRBY"><a href="#DECRBY" class="headerlink" title="DECRBY"></a>DECRBY</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DECRBY key decrement</span><br></pre></td></tr></table></figure>

<blockquote>
<p>同 <code>INCRBY</code></p>
</blockquote>
<h5 id="MSET"><a href="#MSET" class="headerlink" title="MSET"></a>MSET</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MSET key value [key value …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.1</p>
<p>时间复杂度： O(N)，其中 N 为被设置的键数量。</p>
<p>同时为多个键设置值。</p>
<p>如果某个给定键已经存在， 那么 <code>MSET</code> 将使用新值去覆盖旧值， 如果这不是你所希望的效果， 请考虑使用 <code>MSETNX</code> 命令， 这个命令只会在所有给定键都不存在的情况下进行设置。</p>
<p><code>MSET</code> 是一个原子性(atomic)操作， 所有给定键都会在同一时间内被设置， 不会出现某些键被设置了但是另一些键没有被设置的情况。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 同时为多个key设值，key可以是重复的</span></span><br><span class="line">MSET key value key1 value key2 value key3 value</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112162347.png" alt="image-20220112162346661"></p>
<h5 id="MSETNX"><a href="#MSETNX" class="headerlink" title="MSETNX"></a>MSETNX</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MSETNX key value [key value …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.1</p>
<p>时间复杂度： O(N)， 其中 N 为被设置的键数量。</p>
<p>当且仅当所有给定键都不存在时， 为所有给定键设置值。</p>
<p>即使只有一个给定键已经存在， <code>MSETNX</code> 命令也会拒绝执行对所有键的设置操作。</p>
<p><code>MSETNX</code> 是一个原子性(atomic)操作， 所有给定键要么就全部都被设置， 要么就全部都不设置， 不可能出现第三种状态。</p>
</blockquote>
<h5 id="MGET"><a href="#MGET" class="headerlink" title="MGET"></a>MGET</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">MGET key [key …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(N) ，其中 N 为给定键的数量。</p>
<p>返回给定的一个或多个字符串键的值。</p>
<p>如果给定的字符串键里面， 有某个键不存在， 那么这个键的值将以特殊值 <code>nil</code> 表示</p>
</blockquote>
<hr>
<h4 id="哈希表"><a href="#哈希表" class="headerlink" title="哈希表"></a>哈希表</h4><blockquote>
<p>Redis hash 是一个 string 类型的 field（字段） 和 value（值） 的映射表，hash 特别适合用于存储对象。</p>
<p>Redis 中每个 hash 可以存储 232 - 1 键值对（40多亿）。</p>
</blockquote>
<h5 id="HSET"><a href="#HSET" class="headerlink" title="HSET"></a>HSET</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># HSET &lt;哈希表的名称&gt; &lt;String类型的字段&gt; &lt;值&gt;</span></span><br><span class="line"><span class="comment"># HSET     key             value</span></span><br><span class="line">HSET key field value</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(1)</p>
<p>将哈希表 <code>hash</code> 中域 <code>field</code> 的值设置为 <code>value</code> 。</p>
<p>如果给定的哈希表并不存在， 那么一个新的哈希表将被创建并执行 <code>HSET</code> 操作。</p>
<p>如果域 <code>field</code> 已经存在于哈希表中， 那么它的旧值将被新值 <code>value</code> 覆盖。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 在哈希表ms中添加字段为name值为marshio的映射，这里以ms为key，值是name和marshio组成的映射，本质上还是key-value的形式</span></span><br><span class="line">HSET ms name marshio</span><br><span class="line"><span class="comment"># 查看</span></span><br><span class="line">HGET ms name</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112170204.png" alt="image-20220112170202904"></p>
<h5 id="HSETNX"><a href="#HSETNX" class="headerlink" title="HSETNX"></a>HSETNX</h5><h5 id="HGET"><a href="#HGET" class="headerlink" title="HGET"></a>HGET</h5><h5 id="HEXISTS"><a href="#HEXISTS" class="headerlink" title="HEXISTS"></a>HEXISTS</h5><h5 id="HDEL"><a href="#HDEL" class="headerlink" title="HDEL"></a>HDEL</h5><h5 id="HLEN"><a href="#HLEN" class="headerlink" title="HLEN"></a>HLEN</h5><h5 id="HSTRLE"><a href="#HSTRLE" class="headerlink" title="HSTRLE"></a>HSTRLE</h5><h5 id="HINCRBY"><a href="#HINCRBY" class="headerlink" title="HINCRBY"></a>HINCRBY</h5><h5 id="HINCRBYFLOAT"><a href="#HINCRBYFLOAT" class="headerlink" title="HINCRBYFLOAT"></a>HINCRBYFLOAT</h5><h5 id="HMSET"><a href="#HMSET" class="headerlink" title="HMSET"></a>HMSET</h5><h5 id="HMGET"><a href="#HMGET" class="headerlink" title="HMGET"></a>HMGET</h5><h5 id="HKEYS"><a href="#HKEYS" class="headerlink" title="HKEYS"></a>HKEYS</h5><h5 id="HVALS"><a href="#HVALS" class="headerlink" title="HVALS"></a>HVALS</h5><h5 id="HGETALL"><a href="#HGETALL" class="headerlink" title="HGETALL"></a>HGETALL</h5><h5 id="HSCAN"><a href="#HSCAN" class="headerlink" title="HSCAN"></a>HSCAN</h5><hr>
<h4 id="列表"><a href="#列表" class="headerlink" title="列表"></a>列表</h4><h5 id="LPUSH"><a href="#LPUSH" class="headerlink" title="LPUSH"></a>LPUSH</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LPUSH key value [value …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
<p>将一个或多个值 <code>value</code> 插入到列表 <code>key</code> 的表头，注意，redis2.4之前不支持多个值插入。</p>
<p>如果有多个 <code>value</code> 值，那么各个 <code>value</code> 值按从左到右的顺序依次插入到表头。</p>
<p> 比如说，对空列表 <code>mylist</code> 执行命令 <code>LPUSH mylist a b c</code> ，列表的值将是 <code>c b a</code> ，这等同于原子性地执行 <code>LPUSH mylist a</code> 、 <code>LPUSH mylist b</code> 和 <code>LPUSH mylist c</code> 三个命令。</p>
<p>如果 <code>key</code> 不存在，一个空列表会被创建并执行 <a target="_blank" rel="noopener" href="http://redisdoc.com/list/lpush.html#lpush">LPUSH</a> 操作。</p>
<p>当 <code>key</code> 存在但不是列表类型时，返回一个错误。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 向列表list中添加1 2 3 4 5 6六个元素 </span></span><br><span class="line">LPUSH list 1 2 3 4 5 6</span><br><span class="line"><span class="comment"># 像列表list中添加 mas ma str num int 5个元素</span></span><br><span class="line">LPUSH list mas ma str num int</span><br><span class="line"><span class="comment"># 查看列表中所有数据</span></span><br><span class="line">LRANGE list 0 -1 </span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112171628.png" alt="image-20220112171626023"></p>
<h5 id="LPUSHX"><a href="#LPUSHX" class="headerlink" title="LPUSHX"></a>LPUSHX</h5><h5 id="RPUSH"><a href="#RPUSH" class="headerlink" title="RPUSH"></a>RPUSH</h5><h5 id="RPUSHX"><a href="#RPUSHX" class="headerlink" title="RPUSHX"></a>RPUSHX</h5><h5 id="LRANGE"><a href="#LRANGE" class="headerlink" title="LRANGE"></a>LRANGE</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">LRANGE key start end</span><br></pre></td></tr></table></figure>

<blockquote>
<p>查看列表范围内的数据</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以上面列表为例，查看列表中所有数据</span></span><br><span class="line">LRANGE list 0 -1 </span><br></pre></td></tr></table></figure>



<h5 id="LPOP"><a href="#LPOP" class="headerlink" title="LPOP"></a>LPOP</h5><h5 id="RPOP"><a href="#RPOP" class="headerlink" title="RPOP"></a>RPOP</h5><h5 id="RPOPLPUSH"><a href="#RPOPLPUSH" class="headerlink" title="RPOPLPUSH"></a>RPOPLPUSH</h5><h5 id="LREM"><a href="#LREM" class="headerlink" title="LREM"></a>LREM</h5><h5 id="LLEN"><a href="#LLEN" class="headerlink" title="LLEN"></a>LLEN</h5><h5 id="LINDEX"><a href="#LINDEX" class="headerlink" title="LINDEX"></a>LINDEX</h5><h5 id="LINSERT"><a href="#LINSERT" class="headerlink" title="LINSERT"></a>LINSERT</h5><h5 id="LSET"><a href="#LSET" class="headerlink" title="LSET"></a>LSET</h5><h5 id="LRANGE-1"><a href="#LRANGE-1" class="headerlink" title="LRANGE"></a>LRANGE</h5><h5 id="LTRIM"><a href="#LTRIM" class="headerlink" title="LTRIM"></a>LTRIM</h5><h5 id="BLPOP"><a href="#BLPOP" class="headerlink" title="BLPOP"></a>BLPOP</h5><h5 id="BRPOP"><a href="#BRPOP" class="headerlink" title="BRPOP"></a>BRPOP</h5><h5 id="BRPOPLPUSH"><a href="#BRPOPLPUSH" class="headerlink" title="BRPOPLPUSH"></a>BRPOPLPUSH</h5><h4 id="集合"><a href="#集合" class="headerlink" title="集合"></a>集合</h4><blockquote>
<p>Redis 的 Set 是 String 类型的无序集合。集合成员是唯一的，这就意味着集合中不能出现重复的数据。</p>
<p>集合对象的编码可以是 intset 或者 hashtable。</p>
<p>Redis 中集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是 O(1)。</p>
<p>集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。</p>
</blockquote>
<h5 id="SADD"><a href="#SADD" class="headerlink" title="SADD"></a>SADD</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SADD key member [member …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度: O(N)， <code>N</code> 是被添加的元素的数量。</p>
<p>将一个或多个 <code>member</code> 元素加入到集合 <code>key</code> 当中，已经存在于集合的 <code>member</code> 元素将被忽略。</p>
<p>假如 <code>key</code> 不存在，则创建一个只包含 <code>member</code> 元素作成员的集合。</p>
<p>当 <code>key</code> 不是集合类型时，返回一个错误。</p>
</blockquote>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 向集合set中添加1 2 3 元素 </span></span><br><span class="line">SADD <span class="built_in">set</span> 1 2 3</span><br><span class="line"><span class="comment"># 向集合set中添加 4 5 6 元素</span></span><br><span class="line">SADD <span class="built_in">set</span>  4 5 6</span><br><span class="line"><span class="comment"># 向集合中添加重复元素 ，会返回一个0表示没有数据被加入集合</span></span><br><span class="line">SADD <span class="built_in">set</span>  4 5 6</span><br><span class="line"><span class="comment"># 查看集合中所有数据</span></span><br><span class="line">SMEMBERS <span class="built_in">set</span> </span><br></pre></td></tr></table></figure>



<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112172925.png" alt="image-20220112172924339"></p>
<h5 id="SISMEMBER"><a href="#SISMEMBER" class="headerlink" title="SISMEMBER"></a>SISMEMBER</h5><h5 id="SPOP"><a href="#SPOP" class="headerlink" title="SPOP"></a>SPOP</h5><h5 id="SRANDMEMBER"><a href="#SRANDMEMBER" class="headerlink" title="SRANDMEMBER"></a>SRANDMEMBER</h5><h5 id="SREM"><a href="#SREM" class="headerlink" title="SREM"></a>SREM</h5><h5 id="SMOVE"><a href="#SMOVE" class="headerlink" title="SMOVE"></a>SMOVE</h5><h5 id="SCARD"><a href="#SCARD" class="headerlink" title="SCARD"></a>SCARD</h5><h5 id="SMEMBERS"><a href="#SMEMBERS" class="headerlink" title="SMEMBERS"></a>SMEMBERS</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SMEMBERS key</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度: O(N)， <code>N</code> 为集合的基数。</p>
<p>返回集合 <code>key</code> 中的所有成员。</p>
<p>不存在的 <code>key</code> 被视为空集合。</p>
</blockquote>
<h5 id="SSCAN"><a href="#SSCAN" class="headerlink" title="SSCAN"></a>SSCAN</h5><h5 id="SINTER"><a href="#SINTER" class="headerlink" title="SINTER"></a>SINTER</h5><h5 id="SINTERSTORE"><a href="#SINTERSTORE" class="headerlink" title="SINTERSTORE"></a>SINTERSTORE</h5><h5 id="SUNION"><a href="#SUNION" class="headerlink" title="SUNION"></a>SUNION</h5><h5 id="SUNIONSTORE"><a href="#SUNIONSTORE" class="headerlink" title="SUNIONSTORE"></a>SUNIONSTORE</h5><h5 id="SDIFF"><a href="#SDIFF" class="headerlink" title="SDIFF"></a>SDIFF</h5><h5 id="SDIFFSTORE"><a href="#SDIFFSTORE" class="headerlink" title="SDIFFSTORE"></a>SDIFFSTORE</h5><hr>
<h4 id="有序集合"><a href="#有序集合" class="headerlink" title="有序集合"></a>有序集合</h4><blockquote>
<p>Redis 有序集合和集合一样也是 string 类型元素的集合,且不允许重复的成员。</p>
<p>不同的是每个元素都会关联一个 double 类型的分数。redis 正是通过分数来为集合中的成员进行从小到大的排序。</p>
<p>有序集合的成员是唯一的,但分数(score)却可以重复。</p>
<p>集合是通过哈希表实现的，所以添加，删除，查找的复杂度都是 O(1)。 集合中最大的成员数为 232 - 1 (4294967295, 每个集合可存储40多亿个成员)。</p>
</blockquote>
<h5 id="ZADD"><a href="#ZADD" class="headerlink" title="ZADD"></a>ZADD</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ZADD key score member [[score member] [score member] …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.2.0</p>
<p>时间复杂度: O(M*log(N))， <code>N</code> 是有序集的基数， <code>M</code> 为成功添加的新成员的数量。</p>
<p>将一个或多个 <code>member</code> 元素及其 <code>score</code> 值加入到有序集 <code>key</code> 当中。</p>
<p>如果某个 <code>member</code> 已经是有序集的成员，那么更新这个 <code>member</code> 的 <code>score</code> 值，并通过重新插入这个 <code>member</code> 元素，来保证该 <code>member</code> 在正确的位置上。</p>
<p><code>score</code> 值可以是整数值或双精度浮点数。</p>
<p>如果 <code>key</code> 不存在，则创建一个空的有序集并执行 <a target="_blank" rel="noopener" href="http://redisdoc.com/sorted_set/zadd.html#zadd">ZADD</a> 操作。</p>
<p>当 <code>key</code> 存在但不是有序集类型时，返回一个错误。</p>
</blockquote>
<h5 id="ZSCORE"><a href="#ZSCORE" class="headerlink" title="ZSCORE"></a>ZSCORE</h5><h5 id="ZINCRBY"><a href="#ZINCRBY" class="headerlink" title="ZINCRBY"></a>ZINCRBY</h5><h5 id="ZCARD"><a href="#ZCARD" class="headerlink" title="ZCARD"></a>ZCARD</h5><h5 id="ZCOUNT"><a href="#ZCOUNT" class="headerlink" title="ZCOUNT"></a>ZCOUNT</h5><h5 id="ZRANGE"><a href="#ZRANGE" class="headerlink" title="ZRANGE"></a>ZRANGE</h5><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ZRANGE key start stop [WITHSCORES]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 1.2.0</p>
<p>时间复杂度: O(log(N)+M)， <code>N</code> 为有序集的基数，而 <code>M</code> 为结果集的基数。</p>
<p>返回有序集 <code>key</code> 中，指定区间内的成员。</p>
<p>其中成员的位置按 <code>score</code> 值递增(从小到大)来排序。</p>
<p>具有相同 <code>score</code> 值的成员按字典序(<a target="_blank" rel="noopener" href="http://en.wikipedia.org/wiki/Lexicographical_order">lexicographical order</a> )来排列。</p>
<p>如果你需要成员按 <code>score</code> 值递减(从大到小)来排列，请使用 [ZREVRANGE key start stop <a target="_blank" rel="noopener" href="http://redisdoc.com/sorted_set/zrevrange.html#zrevrange">WITHSCORES]</a> 命令。</p>
<p>下标参数 <code>start</code> 和 <code>stop</code> 都以 <code>0</code> 为底，也就是说，以 <code>0</code> 表示有序集第一个成员，以 <code>1</code> 表示有序集第二个成员，以此类推。 你也可以使用负数下标，以 <code>-1</code> 表示最后一个成员， <code>-2</code> 表示倒数第二个成员，以此类推。</p>
<p>超出范围的下标并不会引起错误。 比如说，当 <code>start</code> 的值比有序集的最大下标还要大，或是 <code>start &gt; stop</code> 时， <a target="_blank" rel="noopener" href="http://redisdoc.com/sorted_set/zrange.html#zrange">ZRANGE</a> 命令只是简单地返回一个空列表。 另一方面，假如 <code>stop</code> 参数的值比有序集的最大下标还要大，那么 Redis 将 <code>stop</code> 当作最大下标来处理。</p>
<p>可以通过使用 <code>WITHSCORES</code> 选项，来让成员和它的 <code>score</code> 值一并返回，返回列表以 <code>value1,score1, ..., valueN,scoreN</code> 的格式表示。 客户端库可能会返回一些更复杂的数据类型，比如数组、元组等。</p>
</blockquote>
<h5 id="ZREVRANGE"><a href="#ZREVRANGE" class="headerlink" title="ZREVRANGE"></a>ZREVRANGE</h5><h5 id="ZRANGEBYSCORE"><a href="#ZRANGEBYSCORE" class="headerlink" title="ZRANGEBYSCORE"></a>ZRANGEBYSCORE</h5><h5 id="ZREVRANGEBYSCORE"><a href="#ZREVRANGEBYSCORE" class="headerlink" title="ZREVRANGEBYSCORE"></a>ZREVRANGEBYSCORE</h5><h5 id="ZRANK"><a href="#ZRANK" class="headerlink" title="ZRANK"></a>ZRANK</h5><h5 id="ZREVRANK"><a href="#ZREVRANK" class="headerlink" title="ZREVRANK"></a>ZREVRANK</h5><h5 id="ZREM"><a href="#ZREM" class="headerlink" title="ZREM"></a>ZREM</h5><h5 id="ZREMRANGEBYRANK"><a href="#ZREMRANGEBYRANK" class="headerlink" title="ZREMRANGEBYRANK"></a>ZREMRANGEBYRANK</h5><h5 id="ZREMRANGEBYSCORE"><a href="#ZREMRANGEBYSCORE" class="headerlink" title="ZREMRANGEBYSCORE"></a>ZREMRANGEBYSCORE</h5><h5 id="ZRANGEBYLEX"><a href="#ZRANGEBYLEX" class="headerlink" title="ZRANGEBYLEX"></a>ZRANGEBYLEX</h5><h5 id="ZLEXCOUNT"><a href="#ZLEXCOUNT" class="headerlink" title="ZLEXCOUNT"></a>ZLEXCOUNT</h5><h5 id="ZREMRANGEBYLEX"><a href="#ZREMRANGEBYLEX" class="headerlink" title="ZREMRANGEBYLEX"></a>ZREMRANGEBYLEX</h5><h5 id="ZSCAN"><a href="#ZSCAN" class="headerlink" title="ZSCAN"></a>ZSCAN</h5><h5 id="ZUNIONSTORE"><a href="#ZUNIONSTORE" class="headerlink" title="ZUNIONSTORE"></a>ZUNIONSTORE</h5><h5 id="ZINTERSTORE"><a href="#ZINTERSTORE" class="headerlink" title="ZINTERSTORE"></a>ZINTERSTORE</h5><hr>
<h3 id="库"><a href="#库" class="headerlink" title="库"></a>库</h3><blockquote>
<p>redis默认会给我们创建16个数据库，每个数据库用index来表示。我们进入redis服务时，会进入默认的index=0的数据库。</p>
</blockquote>
<h4 id="切换库"><a href="#切换库" class="headerlink" title="切换库"></a>切换库</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># select &lt;index&gt;</span></span><br><span class="line"><span class="comment"># 进入index=2的数据库，redis中数据库下标是从0开始得</span></span><br><span class="line">select 2</span><br></pre></td></tr></table></figure>

<h4 id="查看数据库"><a href="#查看数据库" class="headerlink" title="查看数据库"></a>查看数据库</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">dbsize <span class="comment"># 返回键值对数量</span></span><br></pre></td></tr></table></figure>

<h4 id="清空数据库"><a href="#清空数据库" class="headerlink" title="清空数据库"></a>清空数据库</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">flushdb <span class="comment"># 返回OK如果成功</span></span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220112112719.png" alt="image-20220112112718579"></p>
<h3 id="查看Redis所有信息"><a href="#查看Redis所有信息" class="headerlink" title="查看Redis所有信息"></a>查看Redis所有信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">info all</span><br><span class="line"><span class="comment"># 出现如下信息</span></span><br></pre></td></tr></table></figure>

<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line">192.168.64.134:6379[2]&gt; info all</span><br><span class="line"><span class="comment"># Server</span></span><br><span class="line">redis_version:6.2.6</span><br><span class="line">redis_git_sha1:00000000</span><br><span class="line">redis_git_dirty:0</span><br><span class="line">redis_build_id:84dfb40eb1119e5</span><br><span class="line">redis_mode:standalone</span><br><span class="line">os:Linux 3.10.0-1160.45.1.el7.x86_64 x86_64</span><br><span class="line">arch_bits:64</span><br><span class="line">multiplexing_api:epoll</span><br><span class="line">atomicvar_api:atomic-builtin</span><br><span class="line">gcc_version:10.2.1</span><br><span class="line">process_id:1</span><br><span class="line">process_supervised:no</span><br><span class="line">run_id:6f32f0e580538801eb39048e7c2b66e18eb09b0f</span><br><span class="line">tcp_port:6379</span><br><span class="line">server_time_usec:1641958240454549</span><br><span class="line">uptime_in_seconds:65000</span><br><span class="line">uptime_in_days:0</span><br><span class="line">hz:10</span><br><span class="line">configured_hz:10</span><br><span class="line">lru_clock:14568288</span><br><span class="line">executable:/data/redis-server</span><br><span class="line">config_file:</span><br><span class="line">io_threads_active:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Clients</span></span><br><span class="line">connected_clients:3</span><br><span class="line">cluster_connections:0</span><br><span class="line">maxclients:10000</span><br><span class="line">client_recent_max_input_buffer:24</span><br><span class="line">client_recent_max_output_buffer:0</span><br><span class="line">blocked_clients:0</span><br><span class="line">tracking_clients:0</span><br><span class="line">clients_in_timeout_table:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Memory</span></span><br><span class="line">used_memory:916464</span><br><span class="line">used_memory_human:894.98K</span><br><span class="line">used_memory_rss:1630208</span><br><span class="line">used_memory_rss_human:1.55M</span><br><span class="line">used_memory_peak:952944</span><br><span class="line">used_memory_peak_human:930.61K</span><br><span class="line">used_memory_peak_perc:96.17%</span><br><span class="line">used_memory_overhead:871760</span><br><span class="line">used_memory_startup:809880</span><br><span class="line">used_memory_dataset:44704</span><br><span class="line">used_memory_dataset_perc:41.94%</span><br><span class="line">allocator_allocated:1000472</span><br><span class="line">allocator_active:1294336</span><br><span class="line">allocator_resident:3657728</span><br><span class="line">total_system_memory:1019588608</span><br><span class="line">total_system_memory_human:972.36M</span><br><span class="line">used_memory_lua:37888</span><br><span class="line">used_memory_lua_human:37.00K</span><br><span class="line">used_memory_scripts:0</span><br><span class="line">used_memory_scripts_human:0B</span><br><span class="line">number_of_cached_scripts:0</span><br><span class="line">maxmemory:0</span><br><span class="line">maxmemory_human:0B</span><br><span class="line">maxmemory_policy:noeviction</span><br><span class="line">allocator_frag_ratio:1.29</span><br><span class="line">allocator_frag_bytes:293864</span><br><span class="line">allocator_rss_ratio:2.83</span><br><span class="line">allocator_rss_bytes:2363392</span><br><span class="line">rss_overhead_ratio:0.45</span><br><span class="line">rss_overhead_bytes:-2027520</span><br><span class="line">mem_fragmentation_ratio:1.87</span><br><span class="line">mem_fragmentation_bytes:756544</span><br><span class="line">mem_not_counted_for_evict:0</span><br><span class="line">mem_replication_backlog:0</span><br><span class="line">mem_clients_slaves:0</span><br><span class="line">mem_clients_normal:61504</span><br><span class="line">mem_aof_buffer:0</span><br><span class="line">mem_allocator:jemalloc-5.1.0</span><br><span class="line">active_defrag_running:0</span><br><span class="line">lazyfree_pending_objects:0</span><br><span class="line">lazyfreed_objects:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Persistence</span></span><br><span class="line">loading:0</span><br><span class="line">current_cow_size:0</span><br><span class="line">current_cow_size_age:0</span><br><span class="line">current_fork_perc:0.00</span><br><span class="line">current_save_keys_processed:0</span><br><span class="line">current_save_keys_total:0</span><br><span class="line">rdb_changes_since_last_save:12</span><br><span class="line">rdb_bgsave_in_progress:0</span><br><span class="line">rdb_last_save_time:1641955871</span><br><span class="line">rdb_last_bgsave_status:ok</span><br><span class="line">rdb_last_bgsave_time_sec:0</span><br><span class="line">rdb_current_bgsave_time_sec:-1</span><br><span class="line">rdb_last_cow_size:196608</span><br><span class="line">aof_enabled:0</span><br><span class="line">aof_rewrite_in_progress:0</span><br><span class="line">aof_rewrite_scheduled:0</span><br><span class="line">aof_last_rewrite_time_sec:-1</span><br><span class="line">aof_current_rewrite_time_sec:-1</span><br><span class="line">aof_last_bgrewrite_status:ok</span><br><span class="line">aof_last_write_status:ok</span><br><span class="line">aof_last_cow_size:0</span><br><span class="line">module_fork_in_progress:0</span><br><span class="line">module_fork_last_cow_size:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Stats</span></span><br><span class="line">total_connections_received:16</span><br><span class="line">total_commands_processed:174</span><br><span class="line">instantaneous_ops_per_sec:0</span><br><span class="line">total_net_input_bytes:7722</span><br><span class="line">total_net_output_bytes:161185</span><br><span class="line">instantaneous_input_kbps:0.00</span><br><span class="line">instantaneous_output_kbps:0.00</span><br><span class="line">rejected_connections:0</span><br><span class="line">sync_full:0</span><br><span class="line">sync_partial_ok:0</span><br><span class="line">sync_partial_err:0</span><br><span class="line">expired_keys:1</span><br><span class="line">expired_stale_perc:0.00</span><br><span class="line">expired_time_cap_reached_count:0</span><br><span class="line">expire_cycle_cpu_milliseconds:921</span><br><span class="line">evicted_keys:0</span><br><span class="line">keyspace_hits:48</span><br><span class="line">keyspace_misses:5</span><br><span class="line">pubsub_channels:0</span><br><span class="line">pubsub_patterns:0</span><br><span class="line">latest_fork_usec:919</span><br><span class="line">total_forks:3</span><br><span class="line">migrate_cached_sockets:0</span><br><span class="line">slave_expires_tracked_keys:0</span><br><span class="line">active_defrag_hits:0</span><br><span class="line">active_defrag_misses:0</span><br><span class="line">active_defrag_key_hits:0</span><br><span class="line">active_defrag_key_misses:0</span><br><span class="line">tracking_total_keys:0</span><br><span class="line">tracking_total_items:0</span><br><span class="line">tracking_total_prefixes:0</span><br><span class="line">unexpected_error_replies:0</span><br><span class="line">total_error_replies:14</span><br><span class="line">dump_payload_sanitizations:0</span><br><span class="line">total_reads_processed:182</span><br><span class="line">total_writes_processed:168</span><br><span class="line">io_threaded_reads_processed:0</span><br><span class="line">io_threaded_writes_processed:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Replication</span></span><br><span class="line">role:master</span><br><span class="line">connected_slaves:0</span><br><span class="line">master_failover_state:no-failover</span><br><span class="line">master_replid:99d6448b92d6f186ba1d013b364c7a5755889de8</span><br><span class="line">master_replid2:0000000000000000000000000000000000000000</span><br><span class="line">master_repl_offset:0</span><br><span class="line">second_repl_offset:-1</span><br><span class="line">repl_backlog_active:0</span><br><span class="line">repl_backlog_size:1048576</span><br><span class="line">repl_backlog_first_byte_offset:0</span><br><span class="line">repl_backlog_histlen:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># CPU</span></span><br><span class="line">used_cpu_sys:119.653536</span><br><span class="line">used_cpu_user:29.059823</span><br><span class="line">used_cpu_sys_children:0.028701</span><br><span class="line">used_cpu_user_children:0.011602</span><br><span class="line">used_cpu_sys_main_thread:119.571257</span><br><span class="line">used_cpu_user_main_thread:29.062662</span><br><span class="line"></span><br><span class="line"><span class="comment"># Modules</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># Commandstats</span></span><br><span class="line">cmdstat_expire:calls=1,usec=6,usec_per_call=6.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_keys:calls=5,usec=53,usec_per_call=10.60,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_flushdb:calls=1,usec=75669,usec_per_call=75669.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_type:calls=15,usec=63,usec_per_call=4.20,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_ttl:calls=14,usec=44,usec_per_call=3.14,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_info:calls=15,usec=19284,usec_per_call=1285.60,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_del:calls=1,usec=11,usec_per_call=11.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_dbsize:calls=3,usec=3,usec_per_call=1.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_llen:calls=4,usec=65,usec_per_call=16.25,rejected_calls=0,failed_calls=2</span><br><span class="line">cmdstat_scan:calls=10,usec=104,usec_per_call=10.40,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_command:calls=4,usec=6582,usec_per_call=1645.50,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_dump:calls=1,usec=24,usec_per_call=24.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_ping:calls=5,usec=588,usec_per_call=117.60,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_get:calls=18,usec=88,usec_per_call=4.89,rejected_calls=7,failed_calls=0</span><br><span class="line">cmdstat_set:calls=12,usec=3434,usec_per_call=286.17,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_select:calls=55,usec=93,usec_per_call=1.69,rejected_calls=0,failed_calls=3</span><br><span class="line">cmdstat_host_:calls=7,usec=1583,usec_per_call=226.14,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_exists:calls=1,usec=3,usec_per_call=3.00,rejected_calls=0,failed_calls=0</span><br><span class="line">cmdstat_move:calls=2,usec=10,usec_per_call=5.00,rejected_calls=0,failed_calls=1</span><br><span class="line"></span><br><span class="line"><span class="comment"># Errorstats</span></span><br><span class="line">errorstat_ERR:count=12</span><br><span class="line">errorstat_WRONGTYPE:count=2</span><br><span class="line"></span><br><span class="line"><span class="comment"># Cluster</span></span><br><span class="line">cluster_enabled:0</span><br><span class="line"></span><br><span class="line"><span class="comment"># Keyspace</span></span><br><span class="line">db0:keys=7,expires=0,avg_ttl=0</span><br></pre></td></tr></table></figure>



<h2 id="HyperLogLog"><a href="#HyperLogLog" class="headerlink" title="HyperLogLog"></a>HyperLogLog</h2><blockquote>
<p>Redis 在 2.8.9 版本添加了 HyperLogLog 结构。</p>
<p>Redis HyperLogLog 是用来做基数统计的算法，HyperLogLog 的优点是，在输入元素的数量或者体积非常非常大时，计算基数所需的空间总是固定 的、并且是很小的。</p>
<p>在 Redis 里面，每个 HyperLogLog 键只需要花费 12 KB 内存，就可以计算接近 2^64 个不同元素的基 数。这和计算基数时，元素越多耗费内存就越多的集合形成鲜明对比。</p>
<p>但是，因为 HyperLogLog 只会根据输入元素来计算基数，而不会储存输入元素本身，所以 HyperLogLog 不能像集合那样，返回输入的各个元素。</p>
</blockquote>
<h2 id="事务"><a href="#事务" class="headerlink" title="事务"></a>事务</h2><blockquote>
<p>Redis 事务可以一次执行多个命令， 并且带有以下三个重要的保证：</p>
<ul>
<li>批量操作在发送 <code>EXEC</code> 命令前被放入队列缓存。</li>
<li>收到 <code>EXEC</code> 命令后进入事务执行，事务中任意命令执行失败，其余的命令依然被执行。</li>
<li>在事务执行过程，其他客户端提交的命令请求不会插入到事务执行命令序列中。</li>
</ul>
</blockquote>
<blockquote>
<p>一个事务从开始到执行会经历以下三个阶段：</p>
<ul>
<li>开始事务。</li>
<li>命令入队。</li>
<li>执行事务。</li>
</ul>
</blockquote>
<h3 id="MULTI"><a href="#MULTI" class="headerlink" title="MULTI"></a>MULTI</h3><blockquote>
<p>开启事务</p>
</blockquote>
<h3 id="EXEC"><a href="#EXEC" class="headerlink" title="EXEC"></a>EXEC</h3><blockquote>
<p>提交事务</p>
</blockquote>
<p>在redis中，以<code>MULIT</code>开启事务，以<code>EXEC</code>结束事务。</p>
<p>举个:chestnut:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 开启事务</span></span><br><span class="line">MULTI</span><br><span class="line"><span class="comment"># 添加值为shiwu的字符串，入列</span></span><br><span class="line">SET key shiwu</span><br><span class="line"><span class="comment"># 添加值为shiwu的字符串，nx（不存在才能添加成功），入列</span></span><br><span class="line">SET key shiwu NX</span><br><span class="line"><span class="comment"># 向集合list中添加数据，入列</span></span><br><span class="line">SADD list </span><br><span class="line"><span class="comment"># 向列表中添加数据</span></span><br><span class="line">LPUSH list-nx</span><br><span class="line"><span class="comment"># 向哈希表中添加数据</span></span><br><span class="line">HSET has</span><br><span class="line"><span class="comment"># 提交事务</span></span><br><span class="line">EXEC</span><br><span class="line"></span><br><span class="line"><span class="comment"># 结果</span></span><br><span class="line">1) OK <span class="comment"># 添加key=shiwu 成功</span></span><br><span class="line">2) (nil) <span class="comment"># 添加key=shiwu 失败</span></span><br><span class="line">3) (error) WRONGTYPE Operation against a key holding the wrong kind of value <span class="comment"># 数据类型不匹配，添加失败</span></span><br><span class="line">4) (<span class="built_in">integer</span>) 3 <span class="comment"># 添加成功</span></span><br><span class="line">5) (<span class="built_in">integer</span>) 4 <span class="comment"># 添加成功</span></span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220113092955.png" alt="image-20220113092953858"></p>
<h2 id="发布订阅"><a href="#发布订阅" class="headerlink" title="发布订阅"></a>发布订阅</h2><blockquote>
<p>Redis 发布订阅 (pub/sub) 是一种消息通信模式：发送者 (pub) 发送消息，订阅者 (sub) 接收消息。</p>
<p>Redis 客户端可以订阅任意数量的频道。</p>
<p>发送者相当于广播台，订阅者相当于正在收看广播的观众，广播台在播放什么，观众就能看到什么。</p>
</blockquote>
<h3 id="订阅频道"><a href="#订阅频道" class="headerlink" title="订阅频道"></a>订阅频道</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 订阅一个名为channel的频道</span></span><br><span class="line">SUBSCRIBE channel [channel...]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(N)，其中 <code>N</code> 是订阅的频道的数量。</p>
<p>订阅给定的一个或多个频道的信息。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 订阅一个或多个符合给定模式的频道</span></span><br><span class="line">PSUBSCRIBE pattern [pattern …]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(N)， <code>N</code> 是订阅的模式的数量。</p>
<p>订阅一个或多个符合给定模式的频道。</p>
<p>每个模式以 <code>*</code> 作为匹配符，比如 <code>it*</code> 匹配所有以 <code>it</code> 开头的频道( <code>it.news</code> 、 <code>it.blog</code> 、 <code>it.tweets</code> 等等)， <code>news.*</code> 匹配所有以 <code>news.</code> 开头的频道( <code>news.it</code> 、 <code>news.global.today</code> 等等)，诸如此类。</p>
</blockquote>
<h3 id="取消订阅"><a href="#取消订阅" class="headerlink" title="取消订阅"></a>取消订阅</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">UNSUBSCRIBE [channel [channel...]]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(N) ， <code>N</code> 是客户端已订阅的频道的数量。</p>
<p>指示客户端退订给定的频道。</p>
<p>如果没有频道被指定，也即是，一个无参数的 <code>UNSUBSCRIBE</code> 调用被执行，那么客户端使用 <code>SUBSCRIBE</code> 命令订阅的所有频道都会被退订。在这种情况下，命令会返回一个信息，告知客户端所有被退订的频道。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">PUNSUBSCRIBE [pattern [pattern …]]</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(N+M) ，其中 <code>N</code> 是客户端已订阅的模式的数量， <code>M</code> 则是系统中所有客户端订阅的模式的数量。</p>
<p>指示客户端退订所有给定模式。</p>
<p>如果没有模式被指定，也即是，一个无参数的 <code>PUNSUBSCRIBE</code> 调用被执行，那么客户端使用 [PSUBSCRIBE pattern <a target="_blank" rel="noopener" href="http://redisdoc.com/pubsub/psubscribe.html#psubscribe">pattern …]</a> 命令订阅的所有模式都会被退订。在这种情况下，命令会返回一个信息，告知客户端所有被退订的模式。</p>
</blockquote>
<h3 id="发布信息"><a href="#发布信息" class="headerlink" title="发布信息"></a>发布信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 向channel频道发送指定信息</span></span><br><span class="line">PUBLISH channel message</span><br></pre></td></tr></table></figure>

<blockquote>
<p>可用版本： &gt;= 2.0.0</p>
<p>时间复杂度： O(N+M)，其中 <code>N</code> 是频道 <code>channel</code> 的订阅者数量，而 <code>M</code> 则是使用模式订阅(subscribed patterns)的客户端的数量。</p>
<p>将信息 <code>message</code> 发送到指定的频道 <code>channel</code> 。</p>
<p>返回接收到信息 <code>message</code> 的订阅者数量。</p>
</blockquote>
<h3 id="查看信息"><a href="#查看信息" class="headerlink" title="查看信息"></a>查看信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 列出当前活跃频道</span></span><br><span class="line">PUBSUB CHANNELS [pattern]</span><br></pre></td></tr></table></figure>

<blockquote>
<p><code>pattern</code> 参数是可选的：</p>
<ul>
<li>如果不给出 <code>pattern</code> 参数，那么列出订阅与发布系统中的所有活跃频道。</li>
<li>如果给出 <code>pattern</code> 参数，那么只列出和给定模式 <code>pattern</code> 相匹配的那些活跃频道。</li>
</ul>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 返回给定频道的订阅者数量， 订阅模式的客户端不计算在内。</span></span><br><span class="line">PUBSUB NUMSUB [channel-1 … channel-N]</span><br></pre></td></tr></table></figure>





<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 查看订阅模式的数量</span></span><br><span class="line">PUBSUB NUMPAT</span><br></pre></td></tr></table></figure>



<p>举个:chestnut:</p>
<p>注意：我们需要两个以上的客户端（redis-cli）。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 第一个客户端向名为channel的频道发布消息，</span></span><br><span class="line">publish channel <span class="string">&quot;channel hello3&quot;</span></span><br><span class="line"><span class="comment"># 返回（integer）1时，只有一个订阅者，返回（integer）2的时候有两个订阅者</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 后面两个客户端订阅名为channel的频道</span></span><br><span class="line">subscribe channel</span><br></pre></td></tr></table></figure>

<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220113085922.png" alt="image-20220113085913513"></p>
<h2 id="持久化"><a href="#持久化" class="headerlink" title="持久化"></a>持久化</h2><h3 id="SAVE"><a href="#SAVE" class="headerlink" title="SAVE"></a>SAVE</h3><blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(N)， <code>N</code> 为要保存到数据库中的 key 的数量。</p>
</blockquote>
<p><a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/save.html#save">SAVE</a> 命令执行一个同步保存操作，将当前 Redis 实例的所有数据快照(snapshot)以 RDB 文件的形式保存到硬盘。</p>
<p>一般来说，在生产环境很少执行 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/save.html#save">SAVE</a> 操作，因为它会阻塞所有客户端，保存数据库的任务通常由 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgsave.html#bgsave">BGSAVE</a> 命令异步地执行。然而，如果负责保存数据的后台子进程不幸出现问题时， <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/save.html#save">SAVE</a> 可以作为保存数据的最后手段来使用。</p>
<p>保存成功时返回 <code>OK</code> 。</p>
<h3 id="BGSAVE"><a href="#BGSAVE" class="headerlink" title="BGSAVE"></a>BGSAVE</h3><blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(N)， <code>N</code> 为要保存到数据库中的 key 的数量。</p>
</blockquote>
<p>在后台异步(Asynchronously)保存当前数据库的数据到磁盘。</p>
<p><a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgsave.html#bgsave">BGSAVE</a> 命令执行之后立即返回 <code>OK</code> ，然后 Redis fork 出一个新子进程，原来的 Redis 进程(父进程)继续处理客户端请求，而子进程则负责将数据保存到磁盘，然后退出。</p>
<p>客户端可以通过 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/lastsave.html#lastsave">LASTSAVE</a> 命令查看相关信息，判断 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgsave.html#bgsave">BGSAVE</a> 命令是否执行成功。</p>
<h3 id="LASTSAVE"><a href="#LASTSAVE" class="headerlink" title="LASTSAVE"></a>LASTSAVE</h3><blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(1)</p>
</blockquote>
<p>返回最近一次 Redis 成功将数据保存到磁盘上的时间，以 UNIX 时间戳格式表示。</p>
<h3 id="BGREWRITEAOF"><a href="#BGREWRITEAOF" class="headerlink" title="BGREWRITEAOF"></a>BGREWRITEAOF</h3><blockquote>
<p>可用版本： &gt;= 1.0.0</p>
<p>时间复杂度： O(N)， <code>N</code> 为要追加到 AOF 文件中的数据数量。</p>
</blockquote>
<p>执行一个 <a target="_blank" rel="noopener" href="http://redis.io/topics/persistence#append-only-file">AOF文件</a> 重写操作。重写会创建一个当前 AOF 文件的体积优化版本。</p>
<p>即使 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 执行失败，也不会有任何数据丢失，因为旧的 AOF 文件在 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 成功之前不会被修改。</p>
<p>重写操作只会在没有其他持久化工作在后台执行时被触发，也就是说：</p>
<ul>
<li>如果 Redis 的子进程正在执行快照的保存工作，那么 AOF 重写的操作会被预定(scheduled)，等到保存工作完成之后再执行 AOF 重写。在这种情况下， <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 的返回值仍然是 <code>OK</code> ，但还会加上一条额外的信息，说明 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 要等到保存操作完成之后才能执行。在 Redis 2.6 或以上的版本，可以使用 [INFO <a target="_blank" rel="noopener" href="http://redisdoc.com/client_and_server/info.html#info">section]</a> 命令查看 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 是否被预定。</li>
<li>如果已经有别的 AOF 文件重写在执行，那么 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 返回一个错误，并且这个新的 <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 请求也不会被预定到下次执行。</li>
</ul>
<p>从 Redis 2.4 开始， AOF 重写由 Redis 自行触发， <a target="_blank" rel="noopener" href="http://redisdoc.com/persistence/bgrewriteaof.html#bgrewriteaof">BGREWRITEAOF</a> 仅仅用于手动触发重写操作。</p>
<blockquote>
<p>Redis 提供了两种数据持久化方式</p>
<ul>
<li>AOF</li>
<li>RDB</li>
</ul>
</blockquote>
<table>
<thead>
<tr>
<th align="center"></th>
<th align="left">RDB</th>
<th>AOF</th>
</tr>
</thead>
<tbody><tr>
<td align="center">开启关闭 | 开启</td>
<td align="left">默认开启。<br />关闭：把配置文件中所有的save都注释，就是关闭了</td>
<td>开启：在配置文件设置<code>appendonly yes</code><br />关闭：在配置文件中设置<code>appendonly no</code>。</td>
</tr>
<tr>
<td align="center">同步机制</td>
<td align="left">可以指定某个时间内发生多少个命令进行同步。比如1分钟内发生了2次命令，就做一次同步。</td>
<td>每秒同步或者每次发生命令后同步</td>
</tr>
<tr>
<td align="center">存储内容</td>
<td align="left">存储的是redis里面的具体的值</td>
<td>存储的是执行的更新数据的操作命令</td>
</tr>
<tr>
<td align="center">存储文件的路径</td>
<td align="left">根据dir以及dbfilename来指定路径和具体的文件名</td>
<td>根据dir以及appendfilename来指定具体的路径和文件名</td>
</tr>
<tr>
<td align="center">优点</td>
<td align="left">（1）存储数据到文件中会进行压缩，文件体积比aof小。<br />（2）因为存储的是redis具体的值，并且会经过压缩，因此在恢复的时候速度比AOF快。<br />（3）非常适用于备份。</td>
<td>（1）AOF的策略是每秒钟或者每次发生写操作的时候都会同步，因此即使服务器故障，最多只会丢失1秒的数据。<br />（2）AOF存储的是Redis命令，并且是直接追加到aof文件后面，因此每次备份的时候只要添加新的数据进去就可以了。<br />（3）如果AOF文件比较大了，那么Redis会进行重写，只保留最小的命令集合。</td>
</tr>
<tr>
<td align="center">缺点</td>
<td align="left">（1）RDB在多少时间内发生了多少写操作的时候就会出发同步机制，因为采用压缩机制，RDB在同步的时候都重新保存整个Redis中的数据，因此你一般会设置在最少5分钟才保存一次数据。在这种情况下，一旦服务器故障，会造成5分钟的数据丢失。<br />（2）在数据保存进RDB的时候，Redis会fork出一个子进程用来同步，在数据量比较大的时候，可能会非常耗时。</td>
<td>（1）AOF文件因为没有压缩，因此体积比RDB大。<br />（2）AOF是在每秒或者每次写操作都进行备份，因此如果并发量比较大，效率可能有点慢。<br />（3）AOF文件因为存储的是命令，因此在灾难恢复的时候Redis会重新运行AOF中的命令，速度不及RDB。</td>
</tr>
</tbody></table>
<h2 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h2><blockquote>
<p>Redis存在的问题最常见的是以下两个，</p>
<ul>
<li>缓存穿透</li>
<li>缓存击穿</li>
</ul>
</blockquote>
<p>想要了解问题，就需要知道缓存是如何工作的。</p>
<p><img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20220121090117.png" alt="Redis"></p>
<h3 id="缓存穿透"><a href="#缓存穿透" class="headerlink" title="缓存穿透"></a>缓存穿透</h3><blockquote>
<p>指的是业务方查询的数据在数据库中不存在，所以缓存中自然也不存在，此时就会查询数据库。此时如果是大量的并发请求，这些请求都会到达数据库，给数据库造成压力。</p>
</blockquote>
<h3 id="缓存击穿"><a href="#缓存击穿" class="headerlink" title="缓存击穿"></a>缓存击穿</h3><blockquote>
<p>指的是业务方向缓存发起请求，缓存中没有，查询数据库，这是正常业务，但是再查询数据库时，数据还没写入缓存的这段时间，大量的并发请求会到达数据库。</p>
</blockquote>

      
    </div><!-- .entry-content -->

    <footer class="entry-meta">
    <a href="/2022/01/11/Redis/">
    <time datetime="2022-01-11T08:39:18.401Z" class="entry-date">
        2022-01-11
    </time>
</a>
    
    
    </footer>
</article>


    
<nav class="nav-single">
    <h3 class="assistive-text">文章导航</h3>
    
        <span class="nav-previous"><a href="/2022/01/18/MQ/" rel="prev"><span class="meta-nav">←</span> 消息中间件</a></span>
    
    
        <span class="nav-next"><a href="/2021/09/15/DateStructor/" rel="next">Date Structor ---  数据结构 <span class="meta-nav">→</span></a></span>
    
</nav><!-- .nav-single -->






<section id="comments">
  <!-- 多说评论框 start -->
  <div class="ds-thread" data-thread-key="post-Redis" data-title="Redis" data-url="https://masuo777.github.io/2022/01/11/Redis/"></div>
  <!-- 多说评论框 end -->
  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
  <script type="text/javascript">
  var duoshuoQuery = {short_name:'ms'};
    (function() {
	  var ds = document.createElement('script');
	  ds.type = 'text/javascript';ds.async = true;
	  ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
	  ds.charset = 'UTF-8';
	  (document.getElementsByTagName('head')[0]
	   || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>
  <!-- 多说公共JS代码 end -->
</section>


</div></div>
        <div id="secondary" class="widget-area" role="complementary">
  
    <aside class="widget">
    <h3 class="widget-title">CONTACT ME</h3>
    <div class="widget-content">
      <p>邮箱：masuo0.0@foxmail.com</p>
      <p>QQ：2919029885</p>
      <a target="_blank" rel="noopener" href="https://www.cnblogs.com/msdog/">博客园</a>
      </br>
      <img src="https://masuo-github-image.oss-cn-beijing.aliyuncs.com/image/20210701223301.jpg" height="250px"/>
      
    </div>
</aside>
  
    <aside class="widget">
    <h3 class="widget-title">赞助我</h3>
    <div class="widget-content">
      <img src='/css/images/alipay.png' style='width:100%;' />
    </div>
</aside>
  
    <aside class="widget">
    <h3 class="widget-title">公众号</h3>
    <div class="widget-content">
      <p>暂时没有，待我代码行数100000，我可能才有这个资格吧。</p>
      <p></p>
    </div>
</aside>
  
</div>
      </div>
      <footer id="colophon" role="contentinfo">
    <p>&copy; 2022 John Doe; All rights reserved.</p>
    <p>Powered by <a href="http://hexo.io/" target="_blank">Hexo</a></p>
</footer>
    <script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"1","bdMiniList":false,"bdPic":"","bdStyle":"2","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>

<script src="/js/jquery-2.0.3.min.js"></script>


  
<link rel="stylesheet" href="/fancybox/jquery.fancybox.css">

  
<script src="/fancybox/jquery.fancybox.pack.js"></script>




<script src="/js/script.js"></script>


<script src="/js/navigation.js"></script>

<div id="bg"></div>

  </div>
</body>
</html>